package cn.bdqn.service.course;

import cn.bdqn.dao.course.CourseMapper;
import cn.bdqn.dao.course.CourseNotesMapper;
import cn.bdqn.pojo.course.CourseNotes;
import cn.bdqn.vo.course.CourseNotesVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CourseNotesService extends ServiceImpl<CourseNotesMapper,CourseNotes> {
    @Autowired
    private CourseNotesMapper courseNotesMapper;

    //查询所有笔记信息
    public PageInfo selectAllCourseNotes(Integer curr, Integer size,String userName,String videoName,String content,String startTime,String endTime){
        System.out.println("start:"+startTime+"endTime:"+endTime);
        PageHelper.startPage(curr,size);
        List<CourseNotesVo> courseNotes = courseNotesMapper.selectNotesAll(userName,videoName,content,startTime,endTime);
        PageInfo page=new PageInfo(courseNotes);
        return page;
    }
    //根据视频id查询笔记信息
    public List<CourseNotes> selectCourseNotesByVideoId(Integer videoId){
        return courseNotesMapper.selectList(new QueryWrapper<CourseNotes>().eq("course_video_id",videoId));
    }
    //根据用户id查询笔记信息
    public List<CourseNotes> selectCourseNotesByUserId(Integer userId){
        return courseNotesMapper.selectList(new QueryWrapper<CourseNotes>().eq("course_notes_user_id",userId));
    }
    //根据创建时间查询笔记信息
    public List<CourseNotes> selectCourseNotesByCreateDate(Date createDate){
        return courseNotesMapper.selectList(new QueryWrapper<CourseNotes>().lt("course_notes_create_date",createDate));
    }
    //根据笔记id修改笔记内容
    @Transactional(rollbackFor = Exception.class)
    public Integer updateCourseNotesById(CourseNotes notes){
        return  courseNotesMapper.update(null,new UpdateWrapper<CourseNotes>().eq("course_notes_id",notes.getCourseNotesId())
                .set("course_notes_content",notes.getCourseNotesContent())
                .set("course_notes_user_id",notes.getCourseNotesUserId())
                .set("course_video_id",notes.getCourseVideoId())
        );
    }
    @Transactional(rollbackFor = Exception.class)
    public Integer updateNotes(CourseNotes notes){
        return courseNotesMapper.updateNotes(notes);
    }

    //根据笔记id删除笔记信息
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteCourseNotesById(Integer notesId){
        return courseNotesMapper.delete(new QueryWrapper<CourseNotes>().eq("course_notes_id",notesId));
    }

    //添加笔记
    @Transactional(rollbackFor = Exception.class)
    public Integer insertCourseNotes(CourseNotes notes){
        return courseNotesMapper.insert(notes);
    }
}
